﻿namespace OleDbDAL.Guest
{
    using DBUtility;
    using IDal.Guest;
    using Model.Guest;
    using System;
    using System.Data;
    using System.Text;

    public class GuestGradeApp : IGuestGrade
    {
        private string dbname = "Systems";

        public void Add(GuestGradeEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into T_GuestGrade(");
            builder.Append("ChainNum,GuestName,Discount,LastUpdateMan,LastUpdateTime");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("" + model.ChainNum + ",");
            builder.Append("'" + model.GuestName + "',");
            builder.Append("" + model.Discount + ",");
            builder.Append("" + model.LastUpdateMan + ",");
            builder.Append("'" + model.LastUpdateTime + "'");
            builder.Append(")");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Delete(int GuestTypeId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from T_GuestGrade ");
            builder.Append(" where GuestTypeId=" + GuestTypeId);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_GuestGrade ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by GuestTypeId ");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId(string swhere)
        {
            return DbHelperOledb.GetMaxID(this.dbname, "GuestTypeId", "T_GuestGrade", swhere);
        }

        public GuestGradeEnt GetModel(int GuestTypeId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select GuestTypeId,ChainNum,GuestName,Discount,LastUpdateMan,LastUpdateTime from T_GuestGrade ");
            builder.Append(" where GuestTypeId=" + GuestTypeId);
            GuestGradeEnt ent = new GuestGradeEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.GuestTypeId = GuestTypeId;
            if (table.Rows.Count > 0)
            {
                if (table.Rows[0]["ChainNum"].ToString() != "")
                {
                    ent.ChainNum = int.Parse(table.Rows[0]["ChainNum"].ToString());
                }
                ent.GuestName = table.Rows[0]["GuestName"].ToString();
                if (table.Rows[0]["Discount"].ToString() != "")
                {
                    ent.Discount = decimal.Parse(table.Rows[0]["Discount"].ToString());
                }
                if (table.Rows[0]["LastUpdateMan"].ToString() != "")
                {
                    ent.LastUpdateMan = int.Parse(table.Rows[0]["LastUpdateMan"].ToString());
                }
                if (table.Rows[0]["LastUpdateTime"].ToString() != "")
                {
                    ent.LastUpdateTime = DateTime.Parse(table.Rows[0]["LastUpdateTime"].ToString());
                }
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_GuestGrade ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "GuestTypeId");
        }

        public void Update(GuestGradeEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_GuestGrade set ");
            builder.Append("ChainNum=" + model.ChainNum + ",");
            builder.Append("GuestName='" + model.GuestName + "',");
            builder.Append("Discount=" + model.Discount + ",");
            builder.Append("LastUpdateMan=" + model.LastUpdateMan + ",");
            builder.Append("LastUpdateTime='" + model.LastUpdateTime + "'");
            builder.Append(" where GuestTypeId=" + model.GuestTypeId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

